package com.leyou.test1.multithread;

public class TestDeam {

    public static void main(String[] args) throws  Exception{
        Object lockObj = new Object();

        new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    synchronized (lockObj) {
                        System.out.println("i = " + i);
                        if (i == 20) {
                            try {
                                System.out.println("i = 20了，我主动释放锁，等待被另一个线程唤醒...");
                                //主动挂起--当前线程不执行了，将锁释放，让其它线程拿到锁
                                lockObj.wait();//进入无限等待状态--等待被另一个线程唤醒
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }.start();

        Thread.sleep(1000 * 2);
        //唤醒线程
        Thread aa=new Thread(){
            @Override
            public void run() {
                System.out.println("唤醒线程启动了...");
                synchronized (lockObj) {
                    System.out.println("唤醒所有在这个锁上等待的线程...");
                    lockObj.notifyAll();
                }
            }
        };

        aa.start();

    }
}